package org.common.utils.net.netty.http.listener;

import org.common.utils.net.netty.http.listener.entity.HandlerRequestAfterListenerInfo;
import org.common.utils.net.netty.http.listener.entity.HandlerRequestBeforeListenerInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 日志监听器
 * @author zhouzhibing
 * @date 2025/11/23 17:54
 */
public class LogListener implements HttpListener.Batch {

    private static final Logger log = LoggerFactory.getLogger(LogListener.class);

    private List<HttpListener<?>> listeners;

    @Override
    public List<HttpListener<?>> getListeners() {
        if(listeners == null) {
            listeners = List.of(new HandlerRequestListener(), new HandlerRequestAfterListener());
        }
        return listeners;
    }

    public static class HandlerRequestListener implements HttpListener<HandlerRequestBeforeListenerInfo> {

        @Override
        public Key getKey() {
            return Key.HANDLER_REQUEST_BEFORE;
        }

        @Override
        public void doListener(HandlerRequestBeforeListenerInfo listener) {
            log.info("doListener HandlerRequestListener：request = {}" , listener.getRequest());
        }
    }

    public static class HandlerRequestAfterListener implements HttpListener<HandlerRequestAfterListenerInfo> {

        @Override
        public Key getKey() {
            return Key.HANDLER_REQUEST_AFTER;
        }

        @Override
        public void doListener(HandlerRequestAfterListenerInfo listener) {
            log.info("doListener HandlerRequestAfterListener：request = {} , result = {}" , listener.getRequest() ,  listener.getResult());
        }
    }
}
